import Vue from 'vue'
import { Toast } from 'vant'

Vue.prototype.window = window
Vue.prototype.document = window.document

// H5调用原生的方法
window.isAndroidOrIOS = Vue.prototype.isAndroidOrIOS = function(obj) {
  try {
    const u = navigator.userAgent
    const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 // android终端
    const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端
    if (isAndroid) {
      console.log('android', JSON.stringify(obj))
      window.android.webToNative(JSON.stringify(obj))
    }
    if (isiOS) {
      console.log('ios', JSON.stringify(obj))
      window.webkit.messageHandlers.webToNative.postMessage(obj)
    }
    return false
  } catch (e) {
    console.log(e)
  }
}

window.logoutByNative = Vue.prototype.logoutByNative = function() {
  Toast.fail('请重新登录')
  isAndroidOrIOS({
    action: 'tokenFailure'
  })
}

window.refreshUserInfo = Vue.prototype.refreshUserInfo = function() {
  isAndroidOrIOS({
    action: 'refreshUserInfo'
  })
}
